home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / librpc-xml-perl / examples / linux.proc.meminfo.code < prev    next >
Encoding:
Text File  |  2007-05-01  |  1.2 KB  |  49 lines

  1. ###############################################################################
  2. #
  3. #   Sub Name:       linux_proc_meminfo
  4. #
  5. #   Description:    Read the /proc/meminfo on a Linux server and return a
  6. #                   STRUCT with the information.
  7. #
  8. #   Arguments:      None.
  9. #
  10. #   Returns:        hashref
  11. #
  12. ###############################################################################
  13. sub linux_proc_meminfo
  14. {
  15.     use strict;
  16.  
  17.     my (%meminfo, $line, $key, @parts);
  18.     local *F;
  19.  
  20.     open(F, '/proc/meminfo') or
  21.         return RPC::XML::fault->new(501, "Cannot open /proc/meminfo: $!");
  22.  
  23.     while (defined($line = <F>))
  24.     {
  25.         next if ($line =~ /^\s+/);
  26.         chomp $line;
  27.  
  28.         @parts = split(/\s+/, $line);
  29.         $key = shift(@parts);
  30.         if ($key eq 'Mem:')
  31.         {
  32.             @meminfo{qw(mem_total mem_used mem_free mem_shared mem_buffers
  33.                         mem_cached)} = @parts;
  34.         }
  35.         elsif ($key eq 'Swap:')
  36.         {
  37.             @meminfo{qw(swap_total swap_used swap_free)} = @parts;
  38.         }
  39.         else
  40.         {
  41.             chop $key; # Lose the trailing ':'
  42.             $meminfo{$key} = join(' ', @parts);
  43.         }
  44.     }
  45.     close(F);
  46.  
  47.     \%meminfo;
  48. }
  49.